

//ad7705/06 for AT89c51 hardware and software design 

#include "reg51.h"
#include "intrins.h"
#include "AD7705.h"

typedef 	unsigned long	u32;

static unsigned char Counter,dis_index,dis_digit;
static unsigned char MSB_Data,LSB_Data,temp0,temp1,temp2,temp3,temp4,temp5;

u32 T_Voltage = 0xffffffff;

enum
{
    /* ?????  RS2 RS1 RS0  */
    REG_COMM    = 0x00,    /* ????? */
    REG_SETUP    = 0x10,    /* ????? */
    REG_CLOCK    = 0x20,    /* ????? */
    REG_DATA    = 0x30,    /* ????? */
    REG_ZERO_CH1    = 0x60,    /* CH1 ????? */
    REG_FULL_CH1    = 0x70,    /* CH1 ?????? */
    REG_ZERO_CH2    = 0x61,    /* CH2 ????? */
    REG_FULL_CH2    = 0x71,    /* CH2 ?????? */

    /* ???? */
    WRITE         = 0x00,    /* ??? */
    READ         = 0x08,    /* ??? */

    /* ?? */
    CH_1        = 0,    /* AIN1+  AIN1- */
    CH_2        = 1,    /* AIN2+  AIN2- */
    CH_3        = 2,    /* AIN1-  AIN1- */
    CH_4        = 3        /* AIN1-  AIN2- */
};

/* ?????bit?? */
enum
{
    MD_NORMAL        = (0 << 6),    /* ???? */
    MD_CAL_SELF        = (1 << 6),    /* ????? */
    MD_CAL_ZERO        = (2 << 6),    /* ??0???? */
    MD_CAL_FULL        = (3 << 6),    /* ??????? */

    GAIN_1            = (0 << 3),    /* ?? */
    GAIN_2            = (1 << 3),    /* ?? */
    GAIN_4            = (2 << 3),    /* ?? */
    GAIN_8            = (3 << 3),    /* ?? */
    GAIN_16            = (4 << 3),    /* ?? */
    GAIN_32            = (5 << 3),    /* ?? */
    GAIN_64            = (6 << 3),    /* ?? */
    GAIN_128        = (7 << 3),    /* ?? */

    /* ???????????????????????,????????????????????? */
    BIPOLAR            = (0 << 2),    /* ????? */
    UNIPOLAR        = (1 << 2),    /* ????? */

    BUF_NO            = (0 << 1),    /* ?????(????????) */
    BUF_EN            = (1 << 1),    /* ????? (???????) */

    FSYNC_0            = 0,
    FSYNC_1            = 1        /* ??? */
};

/* ?????bit?? */
enum
{
    CLKDIS_0    = 0x00,        /* ?????? (??????,????????) */
    CLKDIS_1    = 0x10,        /* ???? (????????,????????MCK_OUT????????? */

    /*
        2.4576MHz(CLKDIV=0 )?? 4.9152MHz (CLKDIV=1 ),CLK ?? "0"?
        1MHz (CLKDIV=0 )? 2MHz   (CLKDIV=1 ),CLK ????  "1"
    */
    CLK_4_9152M = 0x08,
    CLK_2_4576M = 0x00,
    CLK_1M         = 0x04,
    CLK_2M         = 0x0C,

    FS_50HZ        = 0x00,
    FS_60HZ        = 0x01,
    FS_250HZ    = 0x02,
    FS_500HZ    = 0x04,

    /*
        ????????????TM7705 ?????
            ??????? 2.4576MHz ?,???????????? 84H,??????????10Hz,??0.1S ????????
            ??????? 1MHz ?,????????????80H, ??????????4Hz, ??0.25S ???????
    */
    ZERO_0        = 0x00,
    ZERO_1        = 0x80
};

unsigned char code Display_code[11]=
            //{0xa0,0xF9,0xc4,0xd0,0x99,0x92,0x82,0xF8,0x80,0x90,0xFF,};
            {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xFF};
						
void Delayms(unsigned char ms) 
  // relay ms
{ 
   		unsigned char i;

        while(ms--)
        {
              for(i = 0; i < 120; i++);
        }
}

void Reset_Channel()
{
			 CD4066_ACONTROL_U6_IK1 = 0;
		   CD4066_BCONTROL_U6_IK2 = 0;
		   CD4066_CCONTROL_U6_IK3 = 0;
		   CD4066_DCONTROL_U6_IK4 = 0;
		 
		   CD4066_ACONTROL_U5_IK5 = 0;
		   CD4066_BCONTROL_U5_IK6 = 0;
		   CD4066_CCONTROL_U5_IK7 = 0;
		   CD4066_DCONTROL_U5_IK8 = 0;
		 
		   CD4066_ACONTROL_U4_IK9 = 0;
		   CD4066_BCONTROL_U4_IK10 = 0;
		   CD4066_CCONTROL_U4_IK11 = 0;
		   CD4066_DCONTROL_U4_IK12 = 0;
}

void Switch_Channel(unsigned char channelUsbNum)
{
	 Reset_Channel();
	
	 switch(channelUsbNum)
	 {
		 case 0:  /* USB Port #1 */
			 CD4066_ACONTROL_U6_IK1 = 1;
			 break;
		 case 1:  /* USB Port #2 */
			 CD4066_BCONTROL_U6_IK2 = 1;
			 break;
		 case 2:  /* USB Port #3 */
			 CD4066_CCONTROL_U6_IK3 = 1;
			 break;
		 case 3:  /* USB Port #4 */
			 CD4066_DCONTROL_U6_IK4 = 1;
			 break;
		 case 4:  /* USB Port #5 */
			 CD4066_ACONTROL_U5_IK5 = 1;
			 break;
		 case 5:  /* USB Port #6 */
			 CD4066_BCONTROL_U5_IK6 = 1;
			 break;
		 case 6:  /* USB Port #7 */
			 CD4066_CCONTROL_U5_IK7 = 1;
			 break;
		 case 7:  /* USB Port #8 */
			 CD4066_DCONTROL_U5_IK8 = 1;
			 break;
		 case 8:  /* USB Port #9 */
			 CD4066_ACONTROL_U4_IK9 = 1;
			 break;
		 case 9:  /* USB Port #10 */
			 CD4066_BCONTROL_U4_IK10 = 1;
			 break;		 
		 case 10: /* USB Port #11 */
			 CD4066_CCONTROL_U4_IK11 = 1;
			 break;
		 case 11: /* USB Port #12 */
			 CD4066_DCONTROL_U4_IK12 = 1;
			 break;
		 default:
			 break;
	 } 
}

void AD7705_Write_Reg(unsigned char Data_byte)
{ 
             unsigned char i;

             AD7705_CS = 0;
             //AD7705_CLK = 1;
              _nop_();
              _nop_();
             for(i = 0; i < 8; i++) 
             {
    	           AD7705_DATA = (bit)(Data_byte & 0x80);
//	               AD7705_CLK = 0;
                    _nop_();
                    _nop_();
                    _nop_();
//                    AD7705_CLK = 1;                  
                    Data_byte <<= 1;
             }
              _nop_();
              AD7705_CS = 1;
}

unsigned char AD7705_Read_Reg()
{ 
         unsigned char i,Data_read;

         AD7705_CS = 0;
//         AD7705_CLK = 1;

         _nop_();

         for(i = 0; i < 8; i++) 
         {
//             AD7705_CLK = 0;
             _nop_();
             _nop_();
             Data_read <<= 1;
             Data_read |= (unsigned char)AD7705_DATA;
             _nop_();
             _nop_();
//             AD7705_CLK = 1;
             _nop_();
             _nop_();
         }
           _nop_();

          AD7705_CS = 1;
          AD7705_DRDY = 1;

          return(Data_read);
}


void AD7705_Start()
{
		 AD7705_Write_Reg(0x20);
		 AD7705_Write_Reg(0x0c);
		 AD7705_Write_Reg(0x10);
		 AD7705_Write_Reg(0x44);
}

#if 0 
	// old way
void AD7705_init()
{
		 TMOD = 0x11;
		 IT0 = 1;
		 PCON=0X00;
		 ET0 = 1;
		 TH1 = 0xFC;
		 TL1 = 0x17;
		 TR0 = 1;
						
		 EA = 1; 
		 P0 = 0xff; P1 = 0xff; P2 = 0xf7; P3 = 0xff;
		 dis_digit = 0x77;
		 dis_index = 0;
		 Counter  = 0;	

		 AD7705_Write_Reg(0x04);
		 AD7705_Write_Reg(0xF0);
		 AD7705_Write_Reg(0x08);
		 AD7705_Write_Reg(0x62);
}
#endif

void AD7705_init()
{
   //RST = 1; /*?????????VCC,????*/
    Delayms(1);
    //RST = 0;
    Delayms(2);
    //RST = 1;
    Delayms(1);      /* ???? */
    Delayms(5);
    /* AD7705??????????????????500us??? */
    AD7705_CS=0;
    AD7705_Write_Reg(0xFF);
    AD7705_Write_Reg(0xFF);
    AD7705_Write_Reg(0xFF);
    AD7705_Write_Reg(0xFF);
    AD7705_CS=1;
    Delayms(5);

    AD7705_Write_Reg(REG_CLOCK | WRITE | CH_1);//???????;?????????
//                            /*???????*/
    AD7705_Write_Reg(CLKDIS_0 | CLK_4_9152M | FS_50HZ);//* ????50Hz */
    AD7705_Write_Reg(REG_SETUP | WRITE | CH_1);//???????,?????????
//                               /*???????*/
    AD7705_Write_Reg(MD_CAL_SELF |GAIN_1 | UNIPOLAR | BUF_EN| FSYNC_0);//* ????? */
//    while(AD7705_DRDY);    //????
}

void AD7705_Read()
{
         AD7705_Write_Reg(0x38);

         MSB_Data = AD7705_Read_Reg();
         LSB_Data = AD7705_Read_Reg();

         T_Voltage =(unsigned int)MSB_Data;
         T_Voltage = T_Voltage<<8;
         T_Voltage = T_Voltage | (unsigned int)LSB_Data;

         temp0 = T_Voltage/100000;
         temp1 = ((T_Voltage-temp0*100000)/10000);
         temp2 = ((T_Voltage-temp0*100000-temp1*10000)/1000);
         temp3 = ((T_Voltage-temp0*100000-temp1*10000-temp2*1000)/100);
         temp4 = (T_Voltage-temp0*100000-temp1*10000-temp2*1000-temp3*100)/10;
         temp5 = (T_Voltage-temp0*100000-temp1*10000-temp2*1000-temp3*100-temp4*10);
}



void time0(void) interrupt 1
{
         TR0 = 0;
         P2 = dis_digit;   //P2.7 --DS8
         P0 = Display_code[dis_index];
           
         TH0 = 0xFC;
         TL0 = 0x17;
         
		 Counter++;
         
		 if(Counter==1)
         {
	          dis_digit = 0x77;
	          dis_index = temp5;
         }
		 else if(Counter==2)
         {
		      dis_digit = 0xb7;
		      dis_index = temp4;
         }
		 else if(Counter==3)
         {
	          dis_digit = 0xd7;
	          dis_index = temp3;

         }
		 else if(Counter==4)
         {
	          dis_digit = 0xe7;
	          dis_index = temp2;
	          Counter = 0;
         }
         
         TR0 = 1;
} 